package com.maoning.product.service.impl;

import com.maoning.product.entity.SkuImagesEntity;
import com.maoning.product.entity.SpuInfoDescEntity;
import com.maoning.product.service.*;
import com.maoning.product.vo.SkuItemSaleAttrVo;
import com.maoning.product.vo.SkuItemVo;
import com.maoning.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maoning.common.utils.PageUtils;
import com.maoning.common.utils.Query;

import com.maoning.product.dao.SkuInfoDao;
import com.maoning.product.entity.SkuInfoEntity;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
    @Autowired
    private SkuImagesService skuImagesService;
    @Autowired
    private SpuInfoDescService spuInfoDescService;
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private SpuInfoService spuInfoService;

    @Autowired
    //把一个线程池注入到容器中,需要的时候直接拿来用
    private ThreadPoolExecutor executor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        final QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();

//        key:
//        catelogId:
//        brandId:
//        min:
//        max:
        final String key = (String) params.get("key");
        if(StringUtils.isNotEmpty(key)){
            queryWrapper.and((w) -> {
                w.eq("sku_id", key).or().like("sku_name", key);
            });
        }
        final String brandId = (String) params.get("brandId");
        if(StringUtils.isNotEmpty(brandId)&&!"0".equals(brandId)){
            queryWrapper.eq("brand_id", brandId);
        }
        final String catelogId = (String) params.get("catelogId");
        if(StringUtils.isNotEmpty(catelogId)&&!"0".equals(catelogId)){
            queryWrapper.eq("catalog_id", catelogId);
        }
        final String min = (String) params.get("min");
        if(StringUtils.isNotEmpty(min)){
            queryWrapper.ge("price", min);
        }
        final String max = (String) params.get("max");
        if(StringUtils.isNotEmpty(max)){
            try {
                final BigDecimal bigDecimal = new BigDecimal(max);
                if(bigDecimal.compareTo(BigDecimal.ZERO) == 1){
                    queryWrapper.le("price", max);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkuInfosBySpuId(Long spuId) {
        final QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId);
        final List<SkuInfoEntity> list = this.list(queryWrapper);
        return list;
    }

//    @Override
//    //在这里把原来的逻辑进行异步编排的优化
//    public SkuItemVo getSkuItemVoById(Long skuId) {
//        final SkuItemVo skuItemVo = new SkuItemVo();
//
//        //1.sku基本信息
//        final SkuInfoEntity byId = this.getById(skuId);
//        final Long spuId = byId.getSpuId();
//        skuItemVo.setInfo(byId);
//        //三级分类id
//        final Long catalogId = byId.getCatalogId();
//        //2.sku图片信息
//        List<SkuImagesEntity> images = skuImagesService.getBySkuId(skuId);
//        skuItemVo.setImages(images);
//        //3.获取sku的归属spu,再查该sku的兄弟级.
//        List<SkuItemSaleAttrVo> saleAttrVos = spuInfoService.getSaleAttrsBySpuId(spuId);
//        skuItemVo.setSaleAttrs(saleAttrVos);
//        //4.获取spu的介绍
//        final SpuInfoDescEntity byId1 = spuInfoDescService.getById(spuId);
//        skuItemVo.setDesp(byId1);
//        //5.获取spu的规则参数信息
//        List<SpuItemAttrGroupVo> groupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
//        skuItemVo.setGroupAttrs(groupVos);
//        //以上最后封装成skuItemVo返回
//        return skuItemVo;
//    }


    @Override
    //在这里把原来的逻辑进行异步编排的优化
    public SkuItemVo getSkuItemVoById(Long skuId) throws ExecutionException, InterruptedException {
        final SkuItemVo skuItemVo = new SkuItemVo();

        //1.sku基本信息

        final CompletableFuture<SkuInfoEntity> future1 = CompletableFuture.supplyAsync(() -> {
            final SkuInfoEntity byId = this.getById(skuId);
            final Long spuId = byId.getSpuId();
            skuItemVo.setInfo(byId);
            return byId;
        }, executor);

        final CompletableFuture<Void> future1_1 = future1.thenAcceptAsync(resp -> {
            //三级分类id
            final Long catalogId = resp.getCatalogId();

            //3.获取sku的归属spu,再查该sku的兄弟级.
            List<SkuItemSaleAttrVo> saleAttrVos = spuInfoService.getSaleAttrsBySpuId(resp.getSpuId());
            skuItemVo.setSaleAttrs(saleAttrVos);
        }, executor);

        final CompletableFuture<Void> future1_2 = future1.thenAcceptAsync(resp -> {
            //4.获取spu的介绍
            final SpuInfoDescEntity byId1 = spuInfoDescService.getById(resp.getSpuId());
            skuItemVo.setDesp(byId1);
        }, executor);

        final CompletableFuture<List<SpuItemAttrGroupVo>> future1_3 = future1.thenApplyAsync(resp -> {
            //5.获取spu的规则参数信息
            List<SpuItemAttrGroupVo> groupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(resp.getSpuId(), resp.getCatalogId());
            skuItemVo.setGroupAttrs(groupVos);
            return groupVos;
        }, executor);


        final CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {
            //2.sku图片信息
            List<SkuImagesEntity> images = skuImagesService.getBySkuId(skuId);
            skuItemVo.setImages(images);
        }, executor);


        CompletableFuture.allOf(future1_1,future1_2,future1_3,future2).get();
        //以上最后封装成skuItemVo返回
        return skuItemVo;
    }

}